package demo.hdz.mycustomview;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.io.InputStream;

import demo.hdz.mycustomview.customview.CustomImageTextView;
import demo.hdz.mycustomview.customview.CustomImageView;
import demo.hdz.mycustomview.utils.Logger;

/**
 * @author hdz
 * @date 2018/11/29
 */
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private int mLineImgCount = 4;
    private int mImgWidth = 0;
    private int mImgHeight = 0;
    private int mImgMargin = 10;
    private int mImgRowMargin = 20;

    private int mImgCount = 0;

    private LinearLayout mMainLayout;
    private LinearLayout mRowLayout = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CustomImageView imgView = (CustomImageView)findViewById(R.id.imgView);
        imgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "自定义ImageView", Toast.LENGTH_SHORT).show();
            }
        });

        CustomImageTextView wifiImgView = (CustomImageTextView)findViewById(R.id.btnWifi);
        wifiImgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "Wifi", Toast.LENGTH_SHORT).show();
            }
        });

        initView();
    }

    private void initView() {
        mMainLayout = (LinearLayout) findViewById(R.id.linearLayout);
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        int height = outMetrics.heightPixels;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            DisplayMetrics metric = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getRealMetrics(metric);
            width = outMetrics.widthPixels;
            height = outMetrics.heightPixels;
        }
        if (width >= height) {
            mLineImgCount = 5;
            mImgMargin = 20;
            mImgRowMargin = 80;
        } else {
            mLineImgCount = 3;
            mImgMargin = 10;
            mImgRowMargin = 80;
        }
        mImgWidth = (width / mLineImgCount) - (mImgMargin * 2);
        Logger.d(TAG+",init: mImgWidth = " + mImgWidth);
        mImgHeight = mImgWidth + 100;

        addImages(getApplicationContext());
    }




    private void addImages(Context context) {
        AssetManager am = getAssets();
        try {
            String[] tmpFileNames = am.list("images");
            for (String fileName : tmpFileNames) {
                Bitmap bitmap = getBitmapFromAssets(context, fileName);
                addImage(context, bitmap, fileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //addOccupyImageView(mRowLayout, mImgCount);
    }

    private void addImage(Context context, Bitmap bitmap, String imageName) {
        if (bitmap == null) {
            return;
        }
        Logger.d(TAG, ""+bitmap.getWidth()+" " + bitmap.getHeight());
        if (mImgCount%mLineImgCount == 0) {
            //addSpaceView(mRowLayout);
            mRowLayout = new LinearLayout(this);
            LinearLayout.LayoutParams rowLP = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            rowLP.setMargins(0, mImgRowMargin, 0, 0);
            mRowLayout.setLayoutParams(rowLP);
            mMainLayout.addView(mRowLayout);
            //addSpaceView(mRowLayout);
        }
        mImgCount++;

        final CustomImageTextView imageView = new CustomImageTextView(this);
        imageView.setImageBitmap(bitmap);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(mImgWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.TOP;
        lp.setMargins(mImgMargin, 0, mImgMargin, 0);
        imageView.setLayoutParams(lp);
        imageView.setText(imageName);
        if (imageName.contains("af8")) {
            Logger.d("============================");
            imageView.setImageBitmapPress(getBitmapFromAssets(context, "af7.png"));
        }
        imageView.setInnerPadding(20);
        imageView.setTextSize(20);
        imageView.setTextColor(Color.BLACK);
        imageView.setTextColorPress(Color.BLUE);
        imageView.setImageSize(mImgWidth-100, mImgWidth-100);

        if (mRowLayout != null) {
            mRowLayout.addView(imageView);
            onClickImageView(imageView, imageName);
        }
    }

    private void onClickImageView(final CustomImageTextView imageView, final String imageName) {
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, imageName, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public Bitmap getBitmapFromAssets(Context context, String fileName){
        Bitmap bitmap = null;
        AssetManager am = context.getAssets();
        try{
            InputStream is = am.open("images/" + fileName);
            bitmap = BitmapFactory.decodeStream(is);
        }catch(Exception e){
            e.printStackTrace();
        }
        return bitmap;
    }
}
